﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataEntity;
using Model;

namespace SWA_View
{
    public partial class DraftForm : Form
    {
        public ChiShuiEntity Entity = null;
        int _ProjectID = 0;
        
        public DraftForm(int id, int ProjectID, ExperimentType Type)
        {
            InitializeComponent();
            _ProjectID = ProjectID;
            
            if (id > 0)
            {
                CommonModel model = new CommonModel();
                Entity = model.Query<ChiShuiEntity>("ID = " + id).FirstOrDefault();
                model = null;
            }
            if (Entity == null)
                Entity = new ChiShuiEntity() { ProjectID = _ProjectID, ExperimentType = Type.ToString() };
            this.SetData(Entity);
        }

        public DraftForm(ChiShuiEntity _Entity)
        {
            InitializeComponent();
            Entity = _Entity;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {    
            try
            {
                GetData();               
            }
            catch (Exception ex)
            {
                MessageBox.Show("请不要输入非数值型字符。");
            }
            this.DialogResult = DialogResult.OK;  
        }

        public ChiShuiEntity GetData()
        {
            if (Entity == null)
                Entity = new ChiShuiEntity() { ProjectID = _ProjectID };
            try
            {
                Entity.ShouChiShui = float.Parse(txtHeadDraft.Text);
                Entity.WeiChiShui = float.Parse(txtTailDraft.Text);
                Entity.DSC = txtNote.Text;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
            return Entity;
        }

        public void SetData(ChiShuiEntity Entity)
        {
            if (Entity == null)
                Entity = new ChiShuiEntity() { ProjectID = _ProjectID  };
            txtHeadDraft.Text = Entity.ShouChiShui + "";
            txtTailDraft.Text = Entity.WeiChiShui + "";
            txtNote.Text = Entity.DSC;
        }

        private void txtRealLength_TextChanged(object sender, EventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb != null)
            {
                try
                {
                    double.Parse(tb.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("只能输入数字");
                    tb.Focus();
                    tb.Text = "0";
                }
            }
        }

    }
}
